import Axios from "axios";

const axios = Axios.create({
  withCredentials: true,
  headers: {
    LGUI: Date.now(),
  },
});

export const get = async (
  url: string,
  config: {
    searchParams?: Record<string, string>;
    headers?: Record<string, string>;
    timeout?: number;
  },
) => {
  let search = "";
  if (config.searchParams) {
    const searchParams = new URLSearchParams(config.searchParams);
    search = `?${searchParams}`;
  }
  return axios.get(`${url}${search}`, {
    headers: {
      "content-type": "application/json",
      ...config.headers,
    },
    timeout: config.timeout,
  });
};

export const post = async (
  url: string,
  config: {
    searchParams?: Record<string, string>;
    data?: any;
    headers?: Record<string, string>;
    timeout?: number;
  },
) => {
  let search = "";
  if (config.searchParams) {
    const searchParams = new URLSearchParams(config.searchParams);
    search = `?${searchParams}`;
  }
  return axios.post(`${url}${search}`, config.data, {
    headers: {
      "content-type": "application/json",
      ...config.headers,
    },
    timeout: config.timeout,
  });
};
